/**
 * 给你一个字符串s和一个字符串数组dictionary,找出并返回dictionary中最长的字符串，该字符串可以通过删除s中的某些字符串得到
 * 例: 输入s = "abpcplea", dictionary = ["ale", "apple", "monkey", "plea"]
 * 输出: "apple"
*/
function findLongestWord(s, dictionary) {
    // 目标字符串
    let str = "";
    // 编辑字符串数组中的每一个元素
    for (let i = 0; i < dictionary.length; i++) {
        // 双指针
        let left = 0;
        let right = 0;
        // 对比当前字符串是否可以通过给定字符串删除得到
        while (left < s.length && right < dictionary[i].length) {
            if (s.charAt(left) === dictionary[i].charAt(right)) {
                right++;
            }
            // 可以得到并且比原来的字符串长度还要长
            if (right === dictionary[i].length && dictionary[i].length > str.length) {
                str = dictionary[i];
            }
            left++;
        }
    }
    return str;
}

console.log(findLongestWord("abpcplea", ["ale", "apple", "monkey", "plea"]))



